(function(undefined){
	var outlogin = {};

	//检查登录
	outlogin.check_login = function() {

		//账号绑定
		$('#account').bind({
			focus:function(){
				this.className = 'inputhover';
			},
			blur:function() {
				this.className = '';
			}
		});

		//密码绑定
		$('#password').bind({
			focus:function(){
				this.className = 'inputhover';
			},
			blur:function() {
				this.className = '';
			}
		});

		//登录按钮
		$('#submit').bind({
			mouseout:function(){
				this.className = 'sl_submitbtn';
			},
			mouseover:function(){
				this.className = 'sl_submitbtnhover';
			}
		});
	}

	//提交登录
	outlogin.submit_login = function(obj) {
		var form_obj = $(obj);

		if(!outlogin.check_account()){
			return false;
		}
		if(!outlogin.check_pwd()){
			return false;
		}
		if(form_obj.data('lock')) {
			return false;
		}

		if(form_obj.find('input:checkbox').attr('checked')) {
			$('#remember').val('1');
		}

		form_obj.data('lock', true);
		form_obj.find('input').blur();

		//注意回调函数后面有 ‘json’
		$.post('/handle.php', form_obj.serialize(), function(data){
			if(!data || data.state==false){
				parent.dialog.finish({
					content:data.msg,
					success:false,
					callback:function() {
						$(obj).data('lock', false);
					}
				});

				return;
			}
			//用户登录成功
			if(data.type=='user' && data.reg_step==999 && prv_url){
				parent.dialog.finish({
					content:data.msg,
					success:true,
					callback:function (){
						// parent.location.reload();
						//parent.location.href = '/user/about';
						if(prv_url.indexOf('register')!=-1){
							parent.location.href = '/user/about';

							return ;
						}
						
						parent.location.href = prv_url;
					}
				});
			}else if(data.type=='user'&& data.reg_step==2){
				//返回上一页
				parent.dialog.finish({
					content:data.msg,
					success:true,
					callback:function (){
						parent.location.href = '/user/reg2';

					}
				});
			}else if(data.type=='user'&& data.reg_step==3){
				//返回上一页
				parent.dialog.finish({
					content:data.msg,
					success:true,
					callback:function (){
						parent.location.href = '/user/reg3';

					}
				});
			}else if(data.type=='user'){
				//返回上一页
				parent.dialog.finish({
					content:data.msg,
					success:true,
					callback:function (){
						// parent.location.reload();
						parent.location.href = '/user/about';

					}
				});
			}
			//居委会登录成功
			if(data.type=='section'){
				parent.dialog.finish({
					content:data.msg,
					success:true,
					callback:function (){
						parent.location.href = '/section/about';
					}
				});
			}
			//商家登录成功
			if(data.type=='company'){
				parent.dialog.finish({
					content:data.msg,
					success:true,
					callback:function (){
						parent.location.href = '/company/index';
					}
				});
			}
			//物管登录成功
			if(data.type=='section2'){
				parent.dialog.finish({
					content:data.msg,
					success:true,
					callback:function (){
						parent.location.href = '/section/about';
					}
				});
			}
		},'json');
		return false;
	}

	/**
	* 提交登录  外部用户登录 重新刷新界面
	* @author yufu 2012-08-27 15:35:38
	*/
	outlogin.get_login = function(obj) {
		var form_obj = $(obj);
		if(!outlogin.check_account()){
			return false;
		}
		if(!outlogin.check_pwd()){
			return false;
		}
		//登录验证码
		if(!outlogin.check_logincode()){
			return false;
		}
		if(form_obj.data('lock')) {
			return false;
		}
		form_obj.data('lock', true);
		form_obj.find('input').blur();

		$.post('/handle.php', form_obj.serializeArray(), function(data){
			data = common_fun_obj.str_to_json(data);
			if(!data || data.state==false){
				dialog.finish({
					content:data.msg,
					success:false,
					callback:function() {
						$(obj).data('lock', false);
					}
				});
				return;
			}

			//用户登录成功
			if(data.type=='user'){
				dialog.finish({
					content:data.msg,
					success:true,
					callback:function (){
						parent.location.reload();
						parent.dialog.close('outlogin');
						//parent.location.href = '/user/about';
					}
				});
			}
		});
		return false;
	}


	//提交注册
	outlogin.submit_reg = function(obj) {
		var form_obj = $(obj);
		var protocol = $("#checkbox");

		$('#account').blur();
		$('#nickname').blur();
		$('#password').blur();
		$('#mobile').blur();

		if($('#s_account').attr('state') == 'false') {
			return false;
		}

		if($('#s_nickname').attr('state') == 'false') {
			return false;
		}

		if($('#s_password').attr('state') == 'false') {
			return false;
		}

		if($('#mobile').val() && $('#s_mobile').attr('state') == 'false') {
			return false;
		}

		if(!protocol.attr("checked")){
			return false;
		}

		if(form_obj.data('lock')) {
			return false;
		}

		//加锁
		form_obj.data('lock', true);

		//离开焦点
		form_obj.find('input').blur();
		form_obj.find('input:submit').attr('disabled', 'disabled');

		//注册
		$.ajax({
			type: 'POST',
			url: '/handle.php',
			data: form_obj.serializeArray(),
			dataType: 'json',
			success: function(data){
				if(!data.state) {
					parent.dialog.finish({
						content: data.msg,
						success: false,
						callback:function() {
							form_obj.find('input:submit').attr('disabled', '');
						}
					});
					return;
				}

				parent.dialog.finish({
					content: '注册成功',
					success: true,
					callback:function(){
						parent.location.reload();
					}
				});
			}
		});

		return false;
	}

	//检查注册
	outlogin.check_reg = function(){
		$('#account').bind({
			focus:function() {
				this.className='inputhover';
			},
			blur:function(){
				this.className='';
				if(!outlogin.check_account()) {
					return;
				}

				$.ajax({
					type:'POST',
					url:'/handle.php',
					data:'h=user/is_email_exist&email=' + this.value,
					dataType:'json',
					async:false,
					success: function(data){
						if (data == undefined || data.state) {
							outlogin.prompt("account", "账户已经存在", false);
							return;
						}

						outlogin.prompt("account", "", true);
					}
				});
			}
		});

		//昵称
		$('#nickname').bind({
			focus:function(){
				this.className = 'inputhover';
			},
			blur:function() {
				this.className = '';
				outlogin.check_nickname();
			}
		});

		//密码
		$('#password').bind({
			focus:function(){
				this.className = 'inputhover';
			},
			blur:function(){
				this.className = '';
				outlogin.check_pwd();
			}
		});

		//手机号
		$('#mobile').bind({
			focus:function(){
				this.className = 'inputhover';
			},
			blur:function(){
				this.className = '';

				if(this.value == "") {
					$('#s_mobile_f').html('可选，活动中奖后，好通知您').removeClass().addClass('sl_note');
					return;
				}

				if(!outlogin.check_mobile()) {
					return;
				}

				$.ajax({
					type:'POST',
					url:'/handle.php',
					data:'h=user/is_bind_mobile&mobile=' + this.value,
					dataType:'json',
					async:false,
					success: function(data){
						if (data == undefined || data.state == 1 || data.state == -1) {
							outlogin.prompt("mobile", "手机号已经绑定过", false);
							return;
						}

						outlogin.prompt("mobile", "", true);
					}
				});
			}
		})
	}

	/**************************************************/
	/**
	* 检查昵称是否正确
	* @param {Object} str
	*/
	outlogin.check_nickname = function(){
		var obj = $("#nickname");
		if(obj.val() == ""){
			outlogin.prompt("nickname", "昵称不能为空", false);
			return false;
		}
		if(obj.val().length<2 || obj.val().length > 6){
			outlogin.prompt("nickname", "昵称长度为2~6个字", false);
			return false;
		}
		//检查昵称必须为中文、字母和数字组成
		if(obj.val().match(/[^\u4E00-\u9FA5a-zA-Z_]/g) != null){
			outlogin.prompt("nickname", "昵称必须由中文、字母和下划线组成", false);
			return false;
		}
		outlogin.prompt("nickname", "", true);
		return true;
	}

	/**
	* 检查密码格式是否正确
	* @param {Object} str
	*/
	outlogin.check_pwd = function(){
		var obj = $("#password");
		if(obj.val() == ""){
			outlogin.prompt("password", "密码不能为空", false);
			return false;
		}
		if(obj.val().length < 6 || obj.val().length > 30){
			outlogin.prompt("password", "密码长度在4~30个字符", false);
			return false;
		}
		outlogin.prompt("password", "", true);
		return true;
	}

	/**
	* 检查账号格式是否正确
	* @param {Object} str
	*/
	outlogin.check_account = function(){
		var obj = $("#account");
		if(obj.val() == ""){
			outlogin.prompt("account", "账号不能为空", false);
			return false;
		}
		if(obj.val().length<6 || obj.val().length > 30){
			outlogin.prompt("account", "账号长度为6~30个字", false);
			return false;
		}
		//  if(!check_obj.unit.type("email", obj.val())){
		//            outlogin.prompt("account", "邮箱格式不正确", false);
		//            return false;
		//        }

		outlogin.prompt("account", "", true);
		return true;
	}

	/**
	* 检查验证码是否填写
	* @param {Object} str
	*/
	outlogin.check_logincode = function(){
		var obj = $("#logincode");
		if(obj.val() == ""){
			outlogin.prompt("logincode", "验证码不能为空", false);
			return false;
		}

		outlogin.prompt("logincode", "", true);
		return true;
	}

	/**
	* 检查手机格式是否正确
	*/
	outlogin.check_mobile = function() {
		var obj = $("#mobile");

		if(obj.val() == "") {
			return true;
		}

		if(obj.val().length != 11) {
			outlogin.prompt("mobile", "手机号位数为11", false);
			return false;
		}

		//验证电话号码手机号码，包含至今所有号段
		var check = /^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$/;

		if(!check.test(obj.val())) {
			outlogin.prompt("mobile", "手机号码错误", false);
			return false;
		}

		return true;
	}

	/**
	* 提示信息
	* @param {Object} obj_id
	* @param {Object} msg
	* @param {Object} success=true 成功或失败，默认为成功
	*/
	outlogin.prompt = function(obj_id, msg, success){

		if(success){
			//成功提示
			$("#"+obj_id).parent().next().hide();
		}else{
			$("#"+obj_id).parent().next().show();
		}

	}

	outlogin.next_code = function(obj){
		obj.src = "/handle.php?h=auth_code/entry&height=25&width=100&size=14&" + Math.random();
	}

	if(window.outlogin == undefined){
		window.outlogin = outlogin;
	}
})();